package com.hns.can.report.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.domain.ReturnResult;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.report.entity.ModTyBadBhvTypeEntity;
import com.hns.can.report.service.LineDrvStandardService;
import com.hns.can.report.vo.LineAreaBadBhvSortVo;
import com.hns.can.report.vo.lineDrvStandardVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 线路驾驶员规范分析-控制类
 *
 * @author # xujf（835264047@qq.com）
 * @date # 2017-02-15 10:25:32
 */
@Controller
@RequestMapping("lineDrvStandard")
public class LineDrvStandardController extends BaseController {

    @Autowired
    private LineDrvStandardService lineDrvStandardService;

    //用于导出-区域不良行为明细
    final private String[] headerNames1 = {"驾驶员", "车牌号", "车辆编号", "营运线路", "所属机构", "方向", "区域类型", "发生区域", "异常行为", "持续时长", "车速(km/h)", "转速(r/min)", "发生时间"};
    final private String[] headerFields1 = new String[]{"drvName", "licPltNo", "carInCd", "lineName", "organName", "lineTy", "areaType", "areaName", "badBhvName", "duration", "canSpeed", "rpm", "badBhvBeginTime"};
    //用于导出-线路驾驶规范
    final private String[] headerNames2 = {"营运线路", "方向", "区域类型", "区域名称", "不良行为", "平均值", "异常阀值", "持续时长阀值"};
    final private String[] headerFields2 = new String[]{"lineName", "lineTy", "areaType", "areaName", "badBhvName", "meanVal", "threVal", "threLasTim"};

    // todo 线路区域不良行为排名

    /**
     * 线路区域不良行为排名
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineAreaBadBhvSortList")
    @ResponseBody
    public ReturnResult getLineAreaBadBhvSortList(lineDrvStandardVo vo) {
        Map<String, Object> rstMap = lineDrvStandardService.getLineAreaBadBhvSortList(vo);

        return new ReturnResult(rstMap);
    }

    /**
     * 线路区域不良行为排名 - 黑点/全部排名/分页
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineAreaBadBhvSort4BPage")
    @ResponseBody
    public ReturnResult getLineAreaBadBhvSort4BPage(lineDrvStandardVo vo) {
        Pagination dataPage = lineDrvStandardService.getLineAreaBadBhvSort4BPage(vo);

        return new ReturnResult(dataPage);
    }

    /**
     * 线路区域不良行为排名 - 站点/全部排名/分页
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineAreaBadBhvSort4SPage")
    @ResponseBody
    public ReturnResult getLineAreaBadBhvSort4SPage(lineDrvStandardVo vo) {
        Pagination dataPage = lineDrvStandardService.getLineAreaBadBhvSort4SPage(vo);

        return new ReturnResult(dataPage);
    }

    /**
     * 线路区域不良行为排名 - 站点区间/全部排名/分页
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineAreaBadBhvSort4BSPage")
    @ResponseBody
    public ReturnResult getLineAreaBadBhvSort4BSPage(lineDrvStandardVo vo) {
        Pagination dataPage = lineDrvStandardService.getLineAreaBadBhvSort4BSPage(vo);

        return new ReturnResult(dataPage);
    }

    // todo 线路区域不良行为统计

    /**
     * 线路区域不良行为统计
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineAreaBadBhvCntList")
    @ResponseBody
    public ReturnResult getLineAreaBadBhvCntList(lineDrvStandardVo vo) {
        List<LineAreaBadBhvSortVo> dataList = lineDrvStandardService.getLineAreaBadBhvCntList(vo);

        return new ReturnResult(dataList);
    }

    // todo 区域不良行为趋势图数据

    /**
     * 区域不良行为趋势图数据
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineAreaBadBhvTrendList")
    @ResponseBody
    public ReturnResult getLineAreaBadBhvTrendList(lineDrvStandardVo vo) {
        Map<String, Object> rstMap = lineDrvStandardService.getLineAreaBadBhvTrendList(vo);

        return new ReturnResult(rstMap);
    }

    // todo 驾驶员不良行为数量排名

    /**
     * 驾驶员不良行为数量排名 - 前5
     *
     * @param vo
     * @return
     */
    @RequestMapping("getDrvBadBhvCntSortList")
    @ResponseBody
    public ReturnResult getDrvBadBhvCntSortList(lineDrvStandardVo vo) {
        List<LineAreaBadBhvSortVo> dataList = lineDrvStandardService.getDrvBadBhvCntSortList(vo);

        return new ReturnResult(dataList);
    }

    /**
     * 驾驶员不良行为数量排名 - 全部/分页
     *
     * @param vo
     * @return
     */
    @RequestMapping("getDrvBadBhvCntSortPage")
    @ResponseBody
    public ReturnResult getDrvBadBhvCntSortPage(lineDrvStandardVo vo) {
        Pagination dataPage = lineDrvStandardService.getDrvBadBhvCntSortPage(vo);

        return new ReturnResult(dataPage);
    }

    // todo 地图行为分布

    /**
     * 地图行为分布
     *
     * @param vo
     * @return
     */
    @RequestMapping("getBadBhvMapData")
    @ResponseBody
    public ReturnResult getBadBhvMapData(lineDrvStandardVo vo) {
        List<LineAreaBadBhvSortVo> dataList = lineDrvStandardService.getBadBhvMapData(vo);

        return new ReturnResult(dataList);
    }

    // todo 时间行为分布

    /**
     * 时间行为分布
     *
     * @param vo
     * @return
     */
    @RequestMapping("getBadBhvTimeDist")
    @ResponseBody
    public ReturnResult getBadBhvTimeDist(lineDrvStandardVo vo) {
        LineAreaBadBhvSortVo rstVo = lineDrvStandardService.getBadBhvTimeDist(vo);

        return new ReturnResult(rstVo);
    }

    // todo 城市不良行为热力图

    /**
     * 城市不良行为热力图
     *
     * @param vo
     * @return
     */
    @RequestMapping("getCityBadBhvTherDiagData")
    @ResponseBody
    public ReturnResult getCityBadBhvTherDiagData(lineDrvStandardVo vo) {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setUserId(user.getId());

        List<LineAreaBadBhvSortVo> dataList = lineDrvStandardService.getCityBadBhvTherDiagData(vo);

        return new ReturnResult(dataList);
    }


    /**
     * 城市不良行为热力图-区域数据统计
     *
     * @param vo
     * @return
     */
    @RequestMapping("getCityBadBhvTherDiagAreaStatis")
    @ResponseBody
    public ReturnResult getCityBadBhvTherDiagAreaStatis(lineDrvStandardVo vo) {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setUserId(user.getId());

        Map rstMap = lineDrvStandardService.getCityBadBhvTherDiagAreaStatis(vo);

        return new ReturnResult(rstMap);
    }

    // todo 获取不良行为列表

    /**
     * 获取不良行为列表
     *
     * @return
     */
    @RequestMapping("getBadBhvList")
    @ResponseBody
    public ReturnResult getBadBhvList() {
        List<ModTyBadBhvTypeEntity> dataList = lineDrvStandardService.getBadBhvList();

        return new ReturnResult(dataList);
    }

    // todo 区域不良行为明细

    /**
     * 区域不良行为明细
     *
     * @param vo
     * @return
     */
    @RequestMapping("getAreaBadBhvDetailPage")
    @ResponseBody
    public ReturnResult getAreaBadBhvDetailPage(lineDrvStandardVo vo) throws Exception {
        Pagination dataPage = lineDrvStandardService.getAreaBadBhvDetailPage(vo);
        GridPage grid = new GridPage(dataPage);

        return getReturnResult(grid);
    }

    /**
     * 导出-区域不良行为明细
     *
     * @param vo
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportAreaBadBhvDetail")
    public void exportAreaBadBhvDetail(lineDrvStandardVo vo, HttpServletResponse response) throws Exception {
        List dataList = lineDrvStandardService.getAreaBadBhvDetail(vo);

        exportFile(response, dataList, vo.getFileTy(), headerNames1, headerFields1, "区域不良行为明细", "区域不良行为明细");
    }

    // todo 线路驾驶规范

    /**
     * 线路驾驶规范
     *
     * @param vo
     * @return
     */
    @RequestMapping("getLineDrvStandardPage")
    @ResponseBody
    public ReturnResult getLineDrvStandardPage(lineDrvStandardVo vo) throws Exception {
        Pagination dataPage = lineDrvStandardService.getLineDrvStandardPage(vo);
        GridPage grid = new GridPage(dataPage);

        return getReturnResult(grid);
    }

    /**
     * 导出-线路驾驶规范
     *
     * @param vo
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportLineDrvStandard")
    public void exportLineDrvStandard(lineDrvStandardVo vo, HttpServletResponse response) throws Exception {
        List dataList = lineDrvStandardService.exportLineDrvStandard(vo);

        exportFile(response, dataList, vo.getFileTy(), headerNames2, headerFields2, "线路驾驶规范", "线路驾驶规范");
    }

    /**
     * 导出文件
     *
     * @param response
     * @param dataList
     * @param fileTy
     * @param headerNames
     * @param headerFields
     * @param showName
     * @param title
     * @throws Exception
     */
    private void exportFile(
            HttpServletResponse response,
            List<BaseEntity> dataList,
            String fileTy,
            String[] headerNames,
            String[] headerFields,
            String showName,
            String title) throws Exception {

        if (PubUtil.isEmpty(dataList)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }

        ExportUtil.exportFiles(
                Arrays.asList(headerNames),
                Arrays.asList(headerFields),
                dataList,
                response,
                fileTy,
                URLEncoder.encode(showName, "UTF-8")
                        + DateTool.getCurrentSysDate("yyyyMMddHHmmss"),
                title
        );
    }

}
